package com.egao.common.remark.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.core.web.PageResult;
import com.egao.common.remark.entity.TArticleRemark;
import com.egao.common.remark.entity.TCourseRemark;
import com.egao.common.remark.entity.TReleaseRemark;
import com.egao.common.remark.entity.dto.CourseRemarkDto;
import com.egao.common.remark.service.ITArticleRemarkService;
import com.egao.common.remark.service.ITCourseRemarkService;
import com.egao.common.remark.service.ITReleaseRemarkService;
import com.egao.common.system.entity.UserTwo;
import com.egao.common.system.service.UserService;
import com.egao.common.system.service.UserTwoService;
import com.egao.common.test.service.CourseService;
import com.egao.common.test.service.MenurService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @auther ZhaoPeng Wang
 * @create 2022-03-21 21:52:39
 * @description 前端控制器
 */
@Controller
@RequestMapping("/release-remark")
@Slf4j
public class TReleaseRemarkController {

    @Autowired
    MenurService menurService;

    @Autowired
    CourseService courseService;

    @Autowired
    UserService userService;

    @Autowired
    UserTwoService userTwoService;

    @Autowired
    ITReleaseRemarkService itReleaseRemarkService;

    @RequestMapping()
    public String view(Model model) {
        model.addAttribute("remarkList", itReleaseRemarkService.list());
        return "remark/release-remark.html";
    }

    /**
     * 分页查询
     */
    @ResponseBody
    @RequestMapping("/page")
    public PageResult<TReleaseRemark> page(HttpServletRequest request) {
        PageParam<TReleaseRemark> pageParam = new PageParam<>(request);
        return new PageResult<>(itReleaseRemarkService.page(pageParam, pageParam.getWrapper()).getRecords(), pageParam.getTotal());
        //return classifyService.listPage(pageParam);  // 使用关联查询
    }



    /**
     * 查询全部
     */
    @ResponseBody
    @RequestMapping("/list")
    public JsonResult list(HttpServletRequest request) {
        PageParam<TReleaseRemark> pageParam = new PageParam<>(request);
        return JsonResult.ok().setData(itReleaseRemarkService.list(pageParam.getOrderWrapper()));
    }

    /**
     * 根据id查询
     */
    @ResponseBody
    @RequestMapping("/get")
    public JsonResult get(Integer id) {
        return JsonResult.ok().setData(itReleaseRemarkService.getById(id));
    }

    /**
     * 根据id查询
     */
    @ResponseBody
    @RequestMapping("/getContent")
    public String getContent(Integer id) {
        String res = "";
        TReleaseRemark byId = itReleaseRemarkService.getById(id);
        if (byId != null) {
            res = byId.getContent();
        }
        return res;
    }

    /**
     * 根据id查询
     */
    @ResponseBody
    @RequestMapping("/getByContent")
    public PageResult<TReleaseRemark> getByContent(String content) {
        QueryWrapper<TReleaseRemark> wrapper = new QueryWrapper<>();
        wrapper.like("content",content);
        PageParam<TReleaseRemark> pageParam = new PageParam<>();
        return new PageResult<>(itReleaseRemarkService.page(pageParam, pageParam.getOrderWrapper(wrapper)).getRecords(), pageParam.getTotal());
    }


    /**
     * 审核
     * @param id
     * @param state
     * @param userId
     * @return
     */
    @ResponseBody
    @RequestMapping("/shenhe")
    public JsonResult ShenHe(Integer id,Integer state,Integer userId) {
        TReleaseRemark remark = itReleaseRemarkService.getById(id);

        UserTwo usertwo = userTwoService.getById(userId);
        remark.setStart(state);

        if (itReleaseRemarkService.updateById(remark)) {
            return JsonResult.ok("添加成功");
        }
        return JsonResult.error("添加失败");
    }

    /**
     * 添加
     */
    @ResponseBody
    @RequestMapping("/save")
    public JsonResult save(String id,String content) {
        log.info("看这里------->: {}",id);
        log.info("看这里------->: {}",content);
        log.info("看这里------->: {}",id.toString());
        StringBuffer sb = new StringBuffer();
        for (char c : id.toCharArray()){
            if (Character.isDigit(c)){
                sb.append(c);
            }
        }
        log.info("看这里------->: {}",sb.toString());
        int remarkId = Integer.parseInt(sb.toString());
        TReleaseRemark parent = itReleaseRemarkService.getById(id);

        TReleaseRemark remark = new TReleaseRemark();
        Subject subject = SecurityUtils.getSubject();
        Object object = subject.getPrincipal();
        Integer userId;
        if (object != null) {
            try {
                userId = ((UserTwo) object).getUserId();
            }catch (ClassCastException e){
                userId = 0;

            }
            remark.setContent(content);
            remark.setName(content);
            remark.setUserId(userId);
            remark.setCreateTime(new Date());
            remark.setParentId(remarkId);
            remark.setStart(0);

            remark.setReleaseId(parent.getReleaseId());
            if (itReleaseRemarkService.save(remark)) {
                return JsonResult.ok("添加成功");
            }else {
                return JsonResult.error("添加失败");
            }
        }else {
            return JsonResult.error("请先登录");
        }

    }

    /**
     * 添加
     */
    @ResponseBody
    @RequestMapping("/saveBatch")
    public JsonResult saveBatch(@RequestParam List<String> ids, String content) {
        log.info("看这里------->: {}",ids.toString());
        log.info("看这里------->: {}",content);
        String s = ids.get(0);
        log.info("看这里------->: {}",s.toString());
        StringBuffer sb = new StringBuffer();
        for (char c : s.toCharArray()){
            if (Character.isDigit(c)){
                sb.append(c);
            }
        }
        log.info("看这里------->: {}",sb.toString());
        int id = Integer.parseInt(sb.toString());
        TReleaseRemark parent = itReleaseRemarkService.getById(id);

        TReleaseRemark remark = new TReleaseRemark();
        Subject subject = SecurityUtils.getSubject();
        Object object = subject.getPrincipal();
        Integer userId;
        if (object != null) {
            try {
                userId = ((UserTwo) object).getUserId();
            }catch (ClassCastException e){
                userId = 0;

            }
            remark.setContent(content);
            remark.setName(content);
            remark.setUserId(userId);
            remark.setCreateTime(new Date());
            remark.setParentId(id);
            remark.setStart(0);
            remark.setReleaseId(parent.getReleaseId());
            if (itReleaseRemarkService.save(remark)) {
                return JsonResult.ok("添加成功");
            }else {
                return JsonResult.error("添加失败");
            }
        }else {
            return JsonResult.error("请先登录");
        }
    }



    /**
     * 删除文章
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public JsonResult deleteArticle(Integer id) {
        if (itReleaseRemarkService.removeById(id)) {
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }


    /**
     * 批量删除
     */
    @ResponseBody
    @RequestMapping("/removeBatch")
    public JsonResult removeBatch(@RequestBody List<Integer> ids) {
        if (itReleaseRemarkService.removeByIds(ids)) {
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }
}

